package cn.itcast.usermanage.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.itcast.usermanage.mapper.NewUserMapper;
import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private NewUserMapper userMapper;
	@Override
	public EasyUIResult queryEasyUiResult(Integer pageNum, Integer pageSize) {
		//在查询方法调用之前,调用分页插件的静态方法,中间最好不要隔任何代码
		PageHelper.startPage(pageNum, pageSize);
		List<User> users = this.userMapper.select(null);
		//初始化pageInfo对象,所有的分页参数都可以在该对象中获取
		PageInfo<User> pageInfo = new PageInfo<>(users);
		EasyUIResult easyUIResult = new EasyUIResult();
		easyUIResult.setTotal(pageInfo.getTotal());
		easyUIResult.setRows(pageInfo.getList());
		return easyUIResult;
	}
	@Override
	public void addUser(User user1, User user2) {
		this.userMapper.insert(user1);
		//int i =10/0;
		this.userMapper.insert(user2);
		
	}
	@Override
	public boolean addUser(User user)throws Exception {
		int count = this.userMapper.insert(user);
		if (count>0) {
			return true;
		}
		return false;
	}
	@Override
	public Boolean deleteUsers(String[] ids) throws Exception {
		int index = this.userMapper.deleteByExample(ids);
		if (index>0) {
			return true;
		}
		return false;
	}
	@Override
	public User queryUserById(Long id) {
		
		return this.userMapper.selectByPrimaryKey(id);
	}
	@Override
	public void saveUser(User user) {
		this.userMapper.insert(user);
		
	}
	@Override
	public void updateUser(User user) {
		this.userMapper.updateByPrimaryKey(user);
		
	}
	@Override
	public Boolean deleteUser(List<Object> ids) {
		Example example = new Example(User.class);
		Criteria criteria = example.createCriteria();
		criteria.andIn("id", ids);
		int count = this.userMapper.deleteByExample(example);
		if(count > 0){
			return true;
		}
		return false;
	}

}
